package com.hzncc.flowable_diboot.flowable.servicetask.payment;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.diboot.core.holder.ThreadLocalHolder;
import com.diboot.core.service.impl.DictionaryServiceExtImpl;
import com.diboot.core.util.ContextHolder;
import com.hzncc.flowable_diboot.contract.entity.SalesContract;
import com.hzncc.flowable_diboot.contract.service.ContractAuditService;
import com.hzncc.flowable_diboot.contract.service.SalesContractService;
import com.hzncc.flowable_diboot.contract.service.impl.SalesContractServiceImpl;
import com.hzncc.flowable_diboot.payment.entity.PaymentContract;
import com.hzncc.flowable_diboot.payment.entity.SapPaymentContract;
import com.hzncc.flowable_diboot.payment.service.PaymentContractService;
import com.hzncc.flowable_diboot.payment.service.SapPaymentContractService;
import com.hzncc.flowable_diboot.payment.service.impl.PaymentContractServiceImpl;
import com.hzncc.flowable_diboot.payment.service.impl.SapPaymentContractServiceImpl;
import com.hzncc.flowable_diboot.payment.vo.PaymentContractDetailVO;
import com.hzncc.flowable_diboot.payment.vo.SapPaymentContractDetailVO;
import jakarta.annotation.Resource;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.flowable.engine.delegate.DelegateExecution;
import org.flowable.engine.delegate.JavaDelegate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.w3c.dom.svg.SVGAnimatedLengthList;

import java.math.BigDecimal;
import java.util.List;

/**
 * 变更销售合同金额
 */
@Service
public class UpdatePaymentSalesContractAmount implements JavaDelegate {

    @Override
    @Transactional
    public void execute(DelegateExecution execution) {

        PaymentContractService paymentContractService = ContextHolder.getBean(PaymentContractServiceImpl.class);
        SalesContractService salesContractService = ContextHolder.getBean(SalesContractServiceImpl.class);
        SapPaymentContractService sapPaymentContractService = ContextHolder.getBean(SapPaymentContractServiceImpl.class);

        String instanceId = execution.getProcessInstanceId();
        LambdaQueryWrapper<PaymentContract> paymentContractQuery = new LambdaQueryWrapper<>();
        paymentContractQuery.eq(PaymentContract::getInstanceId,instanceId).eq(PaymentContract::isDeleted,0);
        ThreadLocalHolder.setIgnoreInterceptor();
        PaymentContractDetailVO paymentContractDetailVO = paymentContractService.getViewObject(paymentContractQuery, PaymentContractDetailVO.class);
        LambdaQueryWrapper<SapPaymentContract> sapPaymentContractQuery = new LambdaQueryWrapper<>();
        sapPaymentContractQuery.eq(SapPaymentContract::getPaymentContractId,paymentContractDetailVO.getId()).eq(SapPaymentContract::isDeleted,0);
        ThreadLocalHolder.setIgnoreInterceptor();
        List<SapPaymentContract> sapPaymentContractList = sapPaymentContractService.getEntityList(sapPaymentContractQuery);
        if (!CollectionUtils.isEmpty(sapPaymentContractList)){
            for (SapPaymentContract sapPaymentContractDetailVO : sapPaymentContractList){
                String sapContractCode = sapPaymentContractDetailVO.getSapContractCode();
                if (StringUtils.isEmpty(sapContractCode)){
                    continue;
                }
                LambdaQueryWrapper<SalesContract> salesContractQuery = new LambdaQueryWrapper<>();
                salesContractQuery.eq(SalesContract::getSapCode,sapContractCode).eq(SalesContract::isDeleted,0);
                SalesContract salesContract = salesContractService.getSingleEntity(salesContractQuery);
                if (ObjectUtils.isEmpty(salesContract)){
                    continue;
                }
                BigDecimal fixedDiscountAmount = StringUtils.isNotEmpty(salesContract.getFixedDiscountAmount())?new BigDecimal(salesContract.getFixedDiscountAmount()):new BigDecimal(0);
                BigDecimal amountReceived = StringUtils.isNotEmpty(salesContract.getAmountReceived())?new BigDecimal(salesContract.getAmountReceived()):new BigDecimal(0);
                BigDecimal amountNotReceived;
                BigDecimal currentCollectionAmount = sapPaymentContractDetailVO.getCurrentCollectionAmount();
                if (ObjectUtils.isNotEmpty(fixedDiscountAmount)&&
                        ObjectUtils.isNotEmpty(amountReceived)&&
                        ObjectUtils.isNotEmpty(currentCollectionAmount)){
                    amountReceived = amountReceived.add(currentCollectionAmount);
                    amountNotReceived = fixedDiscountAmount.subtract(amountReceived);
                    String paymentStatus = "1";
                    if (amountReceived.compareTo(new BigDecimal(0))>0&&amountNotReceived.compareTo(new BigDecimal(0))>0){
                        paymentStatus = "2";
                    }else if (amountNotReceived.compareTo(new BigDecimal(0))<=0){
                        paymentStatus = "3";
                    }
                    LambdaUpdateWrapper<SalesContract> salesContractUpdate = new LambdaUpdateWrapper<>();
                    salesContractUpdate.set(SalesContract::getAmountReceived,amountReceived)
                            .set(SalesContract::getAmountNotReceived,amountNotReceived)
                            .set(SalesContract::getPaymentStatus,paymentStatus)
                            .eq(SalesContract::getSapCode,sapPaymentContractDetailVO.getSapContractCode()).eq(SalesContract::isDeleted,0);
                    ThreadLocalHolder.setIgnoreInterceptor();
                    salesContractService.updateEntity(salesContractUpdate);
                    sapPaymentContractDetailVO.setReceivedAmount(amountReceived).setUnpaidAmount(amountNotReceived);
                    ThreadLocalHolder.setIgnoreInterceptor();
                    sapPaymentContractService.updateEntity(sapPaymentContractDetailVO);
                }


            }
        }
    }
}
